﻿Public Class RIGHTANDRECEIPT
    Dim connect As CONDB = CONDB.NewConnection
    Dim s As FILTERCLASS
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub MTRGTIDTxt_Click(sender As Object, e As EventArgs) Handles MTRGTIDTxt.Click
        MTRGTIDTxt.Text = ""
        MTRGTIDTxt.Tag = ""
    End Sub

    Private Sub STRGTIDTxt_Click(sender As Object, e As EventArgs) Handles STRGTIDTxt.Click
        STRGTIDTxt.Text = ""
        STRGTIDTxt.Tag = ""
    End Sub

    Private Sub SBRGTIDTxt_Click(sender As Object, e As EventArgs) Handles SBRGTIDTxt.Click
        SBRGTIDTxt.Text = ""
        SBRGTIDTxt.Tag = ""
    End Sub

    Private Sub TMPIDTxt_Click(sender As Object, e As EventArgs) Handles TMPIDTxt.Click
        TMPIDTxt.Text = ""
        TMPIDTxt.Tag = ""
    End Sub

    Private Sub RIGHTANDRECEIPT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridRgtonbill()
        SearchMTRGTIDTxt()
        'SearchSTRGTIDTxt()
        'SearchSBRGTIDTxt()
        SearchTMPIDTxt()

        DesignForm(Me)
        DesignGrid(RGTONBILLGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridRgtonbill()
        Dim objSelectDataRgtonbill As New RGTONBILLCLASS
        Dim Rgtonbill As DataTable
        Rgtonbill = connect.GetTable(objSelectDataRgtonbill.SelectRgtonbill_)
        RGTONBILLGrid.PrimaryGrid.DataSource = Rgtonbill
    End Sub

    Private Sub cleardata()
        MTRGTIDTxt.Text = ""
        STRGTIDTxt.Text = ""
        SBRGTIDTxt.Text = ""
        TMPIDTxt.Text = ""
        rgtonbillstatuscheck.Checked = False
    End Sub

    Private Sub SearchMTRGTIDTxt()
        Dim sql2 As String
        sql2 = "SELECT CAST(mtrgtid AS CHAR(4)) AS MTRGTID, mtrgtname, sdet FROM masmtypergt WHERE status = '1' "
        s = New FILTERCLASS(MTRGTIDTxt, sql2, "รหัส,คู่สัญญาหลัก,มีกลุ่มย่อย", "30,150,44", "1,1,1", "1,1,0")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub

    Private Sub SearchTMPIDTxt()
        Dim sql2 As String
        sql2 = "SELECT cast(`tmpid` as nchar(3)) as tmpid,`tempname`FROM tempbilling WHERE `status` = 1 "
        s = New FILTERCLASS(TMPIDTxt, sql2, "รหัส,แบบหมวดลำดับใบเสร็จ", "70,123", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub

    Private Sub MTRGTIDTxt_TextChanged(sender As Object, e As EventArgs) Handles MTRGTIDTxt.TextChanged
        Dim sdetMTRGT As Boolean
        Dim sdetTable As DataTable = connect.GetTable("SELECT sdet FROM masmtypergt WHERE mtrgtid = '" & Convert.ToString(MTRGTIDTxt.Tag).Trim & "'")
        If sdetTable.Rows.Count > 0 Then
            sdetMTRGT = sdetTable.Rows(0)(0).ToString
        End If

        If sdetMTRGT = True Then
            STRGTIDTxt.Enabled = True


            Dim sql2 As String = "SELECT CAST(strgtid AS CHAR(4)) AS STRGTID, strgtname, sdet FROM masstypergt WHERE status = '1' AND mtrgtid = '" & Convert.ToString(MTRGTIDTxt.Tag).Trim & "'"
            s = New FILTERCLASS(STRGTIDTxt, sql2, "รหัส,คู่สัญญารอง,มีกลุ่มย่อย", "30,150,44 ", "1,1,1", "1,1,0")
            's.SetShowInDown = False
            s.SetShowBorder = True
        Else
            STRGTIDTxt.Text = ""
            SBRGTIDTxt.Text = ""
            STRGTIDTxt.Enabled = False
            SBRGTIDTxt.Enabled = False
        End If
    End Sub

    Private Sub STRGTIDTxt_TextChanged(sender As Object, e As EventArgs) Handles STRGTIDTxt.TextChanged
        Dim sdetSTRGT As Boolean
        Dim sdetTable As DataTable = connect.GetTable("SELECT sdet FROM masstypergt WHERE strgtid = '" & Convert.ToString(STRGTIDTxt.Tag).Trim & "'")
        If sdetTable.Rows.Count > 0 Then
            sdetSTRGT = sdetTable.Rows(0)(0).ToString
        End If

        If sdetSTRGT = True Then
            SBRGTIDTxt.Enabled = True

            Dim s3 As FILTERCLASS
            Dim sql3 As String = "SELECT CAST(sbrgtid AS CHAR(4)) AS SBRGTID, subrgtname FROM massubrgt WHERE status = '1' AND strgtid = '" & Convert.ToString(STRGTIDTxt.Tag).Trim & "'"
            s3 = New FILTERCLASS(SBRGTIDTxt, sql3, "รหัส,คู่สัญญาย่อย", "30,194", "1,1", "1,1")
            's.SetShowInDown = False
            s3.SetShowBorder = True
        Else
            SBRGTIDTxt.Enabled = False
            SBRGTIDTxt.Text = ""
        End If
    End Sub

    Private Sub SuperGridControl1GetCellStyle(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridGetCellStyleEventArgs)
        If e.GridCell.GridColumn.Name.Equals("ลำดับการแสดง") = True Then
            ''If CStr(e.GridCell.Value).Equals("True") = True Then
            'e.GridCell.GridRow.CellStyles.Default.Background.Color1 = Color.Orange
            'e.GridCell.GridRow.Item(3).CellStyles.Default.Background.Color1 = Color.Orange
            ''End If
        End If
    End Sub

    Private Sub RGTONBILLGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles RGTONBILLGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value() IsNot DBNull.Value Then
            RBILLIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            RBILLIDTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("mtrgtid").Value() IsNot DBNull.Value Then
            MTRGTIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("mtrgtid").Value()
        Else
            MTRGTIDTxt.Tag = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คู่สัญญาหลัก").Value() IsNot DBNull.Value Then
            MTRGTIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คู่สัญญาหลัก").Value()
        Else
            MTRGTIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("strgtid").Value() IsNot DBNull.Value Then
            STRGTIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("strgtid").Value()
        Else
            STRGTIDTxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คู่สัญญารอง").Value() IsNot DBNull.Value Then
            STRGTIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คู่สัญญารอง").Value()
        Else
            STRGTIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("sbrgtid").Value() IsNot DBNull.Value Then
            SBRGTIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("sbrgtid").Value()
        Else
            SBRGTIDTxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คู่สัญญาย่อย").Value() IsNot DBNull.Value Then
            SBRGTIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("คู่สัญญาย่อย").Value()
        Else
            SBRGTIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("tmpid").Value() IsNot DBNull.Value Then
            TMPIDTxt.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("tmpid").Value()
        Else
            TMPIDTxt.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แบบหมวดลำดับใบเสร็จ").Value() IsNot DBNull.Value Then
            TMPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("แบบหมวดลำดับใบเสร็จ").Value()
        Else
            TMPIDTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            rgtonbillstatuscheck.Checked = True
        Else
            rgtonbillstatuscheck.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE rgtonbill SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `rbillid` = '" & RBILLIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        'If STRGTIDTxt.Text = "" Then
        '    STRGTIDTxt.Enabled = False
        'Else
        '    STRGTIDTxt.Enabled = True
        'End If
        'If SBRGTIDTxt.Text = "" Then
        '    SBRGTIDTxt.Enabled = False
        'Else
        '    SBRGTIDTxt.Enabled = True
        'End If
        addrgtonbill.Enabled = False
        'Dim sql2 As String = "SELECT CAST(STRGTID AS CHAR(4)) AS STRGTID, STRGTNAME, SDET FROM masstypergt WHERE STATUS = '1' AND MTRGTID = '" & Convert.ToString(MTRGTIDTxt.Tag).Trim & "'"
        's.SetSql = sql2
        ''s.SetShowInDown = False
        's.SetShowBorder = True
        'Dim sql3 As String = "SELECT CAST(SBRGTID AS CHAR(4)) AS SBRGTID, SUBRGTNAME FROM massubrgt WHERE STATUS = '1' AND STRGTID = '" & Convert.ToString(STRGTIDTxt.Tag).Trim & "'"
        's = New FILTERCLASS(SBRGTIDTxt, sql3, "รหัส,คู่สัญญาย่อย", "30,194", "1,1", "1,1")
        's.SetShowInDown = False
        's.SetShowBorder = True
    End Sub

    Private Sub addrgtonbill_Click(sender As Object, e As EventArgs) Handles addrgtonbill.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If TMPIDTxt.Text.Length > 0 And MTRGTIDTxt.Text.Length > 0 Then
                    Dim objInsertData As New RGTONBILLCLASS
                    If Convert.ToString(MTRGTIDTxt.Tag).Trim <> "" Then
                        objInsertData.MTRGTID_ = Convert.ToString(MTRGTIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(STRGTIDTxt.Tag).Trim <> "" Then
                        objInsertData.STRGTID_ = Convert.ToString(STRGTIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(SBRGTIDTxt.Tag).Trim <> "" Then
                        objInsertData.SBRGTID_ = Convert.ToString(SBRGTIDTxt.Tag).Trim
                    End If
                    If Convert.ToString(TMPIDTxt.Tag).Trim <> "" Then
                        objInsertData.TMPID_ = Convert.ToString(TMPIDTxt.Tag).Trim
                    End If
                    objInsertData.STATUS_ = rgtonbillstatuscheck.Checked
                    objInsertData.insertRgtonbillData()

                    SelectDatagridRgtonbill()

                    cleardata()
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editrgtonbill_Click(sender As Object, e As EventArgs) Handles editrgtonbill.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If TMPIDTxt.Text.Length > 0 And MTRGTIDTxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objEditData As New RGTONBILLCLASS
                        If Convert.ToString(RBILLIDTxt.Text) <> "" Then
                            objEditData.RBILLID_ = Convert.ToString(RBILLIDTxt.Text)
                        End If
                        If Convert.ToString(MTRGTIDTxt.Tag) <> "" Then
                            objEditData.MTRGTID_ = Convert.ToString(MTRGTIDTxt.Tag)
                        End If
                        If Convert.ToString(STRGTIDTxt.Tag) <> "" Then
                            objEditData.STRGTID_ = Convert.ToString(STRGTIDTxt.Tag)
                        End If
                        If Convert.ToString(SBRGTIDTxt.Tag) <> "" Then
                            objEditData.SBRGTID_ = Convert.ToString(SBRGTIDTxt.Tag)
                        End If
                        If Convert.ToString(TMPIDTxt.Tag) <> "" Then
                            objEditData.TMPID_ = Convert.ToString(TMPIDTxt.Tag)
                        End If
                        objEditData.STATUS_ = rgtonbillstatuscheck.Checked
                        objEditData.editrgtonbilldata()

                        SelectDatagridRgtonbill()

                        cleardata()
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                addrgtonbill.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("maslocationd")
        nextform.ShowDialog()
    End Sub

    Private Sub exportExcel_Click(sender As Object, e As EventArgs) Handles exportExcel.Click
        Dim nextform As New IMPORTEXCEL("maslocationd")
        nextform.ShowDialog()
    End Sub

   
    
End Class